package com.baizhi.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class BesnFactory {
    private static Properties p=new Properties();
    static {
        //用静态代码在类加载的时候执行该代码 通过类对象的根目录获取applicationContext.properties
        InputStream stream = BesnFactory.class.getResourceAsStream("/applicationContext.properties");
        try {
            p.load(stream);
            stream.close();
        } catch (IOException e) {
            throw  new RuntimeException();
        }
    }
    //定义获取对象的方法
    public static Object getBean(String id){
        //通过配置文件创建对象
        try {
            Class name = Class.forName(p.getProperty(id));
            return name.newInstance();//通newInstance方法调用无参构造创建对象
        } catch (Exception e) {
            throw  new RuntimeException();
        }
    }
}
